Xbasic

*property_to_xml Function

Syntax

dim resultXML as C = *PROPERTY_TO_XML(prop as p,topTag as c[,flags as c])

Arguments

propPointer

The dot variable to convert to XML.

topTagCharacter

The name of the top-level XML tag.

flagsCharacter

Optional flags.

Returns

resultXMLCharacter

Returns an XML representation of the dot variable.

Description

Save property to XML.

DIM test as P
test.namefirst = "John"
test.namelast = "Smith"
test.otherxml =<<%xml%
<note>
<to>Dion</to>
<from>Dave</from>
<heading>Reminder</heading>
<body>Don't forget Wednesday's Q&mp;A session!</body>
</note>
%xml%

DIM output  as C
output = *property_to_xml(test,"test")
? output
= <test
namefirst = "John"
namelast = "Smith"
otherxml = "&lt;note&gt;
&lt;to&gt;Dion&lt;/to&gt;
&lt;from&gt;Dave&lt;/from&gt;
&lt;heading&gt;Reminder&lt;/heading&gt;
&lt;body&gt;Don't forget Wednesday's Q&amp;mp;A session!&lt;/body&gt;
&lt;/note&gt;
" />

Understanding __A5_elementContent and __A5_Xml_Manifest

Some meta information is included when XML content is converted to a dot variable. This information is required in order to provide round-trip support (convert XML to a property array and then back to original XML format.) Consider the following very simple snippet of XML.

xml = <<%txt%
<name city-name="boston">
    Fred Smith
</name>
%txt%

If you view this XML using the XML viewer function (showXML()), you will see this:

showXML(xml)
images/parseXML1.jpg

You will notice that the XML has an attribute called 'city-name'. This is not a valid Xbasic variable name, so the attribute has to be renamed (to 'city_name'). In order that the *property_to_xml() function can get back to the same XML that was originally parsed, a list of all of the attribute names that were changed is kept in the special __A5_Xml_Manifest property.

images/parseXML2.jpg

Also, you will notice that in the XML snippet, the 'name' element has a value and also it has attributes. The attribute values are shown as properties and the element value is shown using the special property name '__A5_elementContent'.

Here is how you can parse the above XML into an Xbasic dot variable:

delete p
dim p as p

'set the optional 3rd flag to .t. to use the special properties
*property_from_xml(xml,p,.t.)

'convert the dot variable into a script so we can 'see' what's in the variable
?*variable_to_script(p)
= DIM name as P
DIM name.city_name as C = "boston"
DIM name.__A5_elementContent as C = <<%str%

Fred Smith
%str%
DIM __A5_Xml_Manifest as C = <<%str%
@Mapping:
city_name=city-name%str%

'now, go back to XML
?*property_to_xml(p,"")
= <name city-name = "boston" >
    Fred Smith
</name>

Notice how this is a perfect 'round-trip'! The generated XML is the same as the initial XML that was parsed. Now, try the above exercise without using the new optional flag on the *property_to_xml() function

delete p
dim p as p

*property_from_xml(xml,p,.f.)

'convert the dot variable into a script so we can 'see' what's in the variable

?*variable_to_script(p)
= DIM name as P
DIM name.city_name as C = "boston"

Notice how the Xbasic dot variable only has the value of the attribute. It does NOT have the value of the element!

See Also